<?php

namespace App\Service;

use App\Model\UtilSmsModel;
use App\Utility\Sms;
use EasySwoole\ORM\Exception\Exception;
use EasySwoole\Utility\Random;
use Throwable;

class SmsService extends BaseService
{
    /**
     * @param $mobile
     * @param $type
     * @return array
     * @throws Exception
     * @throws Throwable
     */
    public function send_sms($mobile, $type): array
    {
        //check
        if (!(preg_match("/^(13[0-9]|14[5|6|7|8|9]|15[0|1|2|3|5|6|7|8|9]|16[2|5|6|7]|17[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|4|5|6|7|8|9]|19[0|1|2|3|5|6|7|8|9])\d{8}$/", $mobile))) {
            return [
                'result' => false,
                'msg' => '手机号码不正确'
            ];
        }

        $time = time() - (3600 * 24);
        $smscount = UtilSmsModel::create()->where("create_time > " . $time)->where(['receive' => $mobile])->where(['type' => $type])->count();
        if ($smscount == 10) {
            return [
                'result' => false,
                'msg' => '当前号码今日获取短信息次数已超限'
            ];
        }
        $sms = UtilSmsModel::create()->where("create_time > " . (time() - 300))->where(['receive' => $mobile, 'type' => $type])->count();
        if ($sms > 0) {
            return [
                'result' => false,
                'msg' => '五分钟内不可重复获取短信验证码'
            ];
        }

        $code = Random::number(6);
        if ($type == 'login') {
            $templete = ConfigService::getInstance()->getSys('templetecode', 'sms');
        } elseif ($type == 'password') {
            $templete = ConfigService::getInstance()->getSys('templeteCheckCode', 'sms');
        } elseif ($type == 'check') {
            $templete = ConfigService::getInstance()->getSys('templeteCheckCode', 'sms');
        } else {
            return [
                'result' => false,
                'msg' => '收取短信类型不正确'
            ];
        }

        $appid = ConfigService::getInstance()->getSys('appkey', 'sms');
        $secret = ConfigService::getInstance()->getSys('appsecret', 'sms');
        $sign = ConfigService::getInstance()->getSys('signname', 'sms');

        $ret = Sms::getInstance()->sendSms($appid, $secret, $mobile, $sign, $templete, $code);
        $ret = json_decode($ret, true);
        if ($ret["Code"] == 'OK') {
            $data = [
                'receive' => $mobile,
                'type' => $type,
                'requestId' => $ret['RequestId'],
                'code' => $code
            ];
            $res = UtilSmsModel::create()->addEmail($data);
            return [
                'result' => true,
                'msg' => '发送成功',
                'data' => $ret
            ];
        } else {
            return [
                'result' => false,
                'msg' => '发送失败'
            ];
        }
    }

    /**
     * @param $requestId
     * @param $code
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception|\Throwable
     */
    public function check_sms($requestId, $code): array
    {
        $sms = UtilSmsModel::create()->getEmail(['requestId' => $requestId]);
        if (!$sms) {
            return [
                'result' => false,
                'msg' => '验证失败'
            ];
        }

        if ($sms->code !== $code) {
            return [
                'result' => false,
                'msg' => '验证码输入错误'
            ];
        }
        if ((time() - $sms->create_time) > 300) {
            return [
                'result' => false,
                'msg' => '当前任务已超时，请重新验证'
            ];
        }
        return [
            'result' => true,
            'msg' => '验证成功',
            'data' => 'success'
        ];
    }
}